package com.ieng.kican.modules.security.core.service;

import com.ieng.kican.common.utils.IUtil;
import com.ieng.kican.modules.security.domain.JwtAuthUser;
import com.ieng.kican.modules.security.utils.SecurityUtils;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component("rbac")
public class AuthorizeService {

    public boolean hasPower(String... names){
        JwtAuthUser current = SecurityUtils.current();
        Set<String> permissions = current.getPermissions();
        for (String name : names) {
            if (permissions.contains(name)) {
                return true;
            }
        }
        return false;
    }

    public boolean hasRole(String... names){
        JwtAuthUser current = SecurityUtils.current();
        Set<String> roles = current.getRoles();
        for (String name : names) {
            if(roles.contains(name)){
                return true;
            }
        }
        return false;
    }

    public final boolean isLogin() {
        try {
            return IUtil.isNotObject(SecurityUtils.current());
        } catch (Exception e) {
            return false;
        }
    }

}
